package wxmod

import (
	"sort"
	"crypto/sha1"
	"io"
	"strings"
	"fmt"
	"log"

	"myproject/wx_test/src/wxglob"
)

func GenHashcode(req Request) (string, error) {
	token := wxglob.Config.Section("wx").Key("token").String()
	list := []string{token, req.TimeStamp, req.Nonce}

	//按照字典序排列
	sort.Strings(list)

	//sha1加密
	hash := sha1.New()

	//strings.join
	_, err := io.WriteString(hash, strings.Join(list, ""))
	if err != nil {
		log.Println(err)
		return "", err
	}
	//生成16位hash
	return fmt.Sprintf("%x", hash.Sum(nil)), nil
}